package cn.tedu.flower.manage.content.controller;

import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.security.CurrentPrincipal;
import cn.tedu.flower.manage.common.web.JsonResult;
import cn.tedu.flower.manage.content.pojo.param.GoodsAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.GoodsUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.GoodsListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.GoodsStandardVO;
import cn.tedu.flower.manage.content.service.IGoodsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/content/goods")
@Validated
@Api(tags = "3.2. 商品管理-商品信息管理")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/content/article/add-new')")
    @ApiOperation("发布商品")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @Valid GoodsAddNewParam goodsAddNewParam) {
        log.debug("开始处理【发布商品】的请求，参数：{}", goodsAddNewParam);
        goodsService.addNew(currentPrincipal, goodsAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("根据ID删除商品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
        log.debug("开始处理【根据ID删除商品】的请求，参数：{}", id);
        goodsService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/content/article/update')")
    @ApiOperation("修改商品信息")
    @ApiOperationSupport(order = 312)
    public JsonResult updateGoods(GoodsUpdateParam goodsUpdateParam){
        log.debug("开始处理【修改商品信息】的请求，参数：{}",goodsUpdateParam);
        goodsService.update(goodsUpdateParam);
        return JsonResult.ok();


    }

    @PostMapping("/{id:[0-9]+}/put-on")
        @PreAuthorize("hasAuthority('/content/article/update')")
        @ApiOperation("上架商品")
        @ApiOperationSupport(order = 310)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
        })
        public JsonResult putOn(@PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
            log.debug("开始处理【上架商品】的请求，参数：{}", id);
            goodsService.putOn(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/pull-off")
    @PreAuthorize("hasAuthority('/content/article/update')")
    @ApiOperation("下架商品")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult pullOff(@PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
        log.debug("开始处理【下架商品】的请求，参数：{}", id);
        goodsService.pullOff(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("根据ID查询商品详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的商品ID值！") Long id) {
        log.debug("开始处理【根据ID查询商品详情】的请求，参数：{}", id);
        GoodsStandardVO queryResult = goodsService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("查询商品列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
    })
    public JsonResult list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询商品列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<GoodsListItemVO> pageData = goodsService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/list-by-category")
    @PreAuthorize("hasAuthority('/content/article/read')")
    @ApiOperation("根据商品类别查询商品列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "商品类别ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult listByCategoryId(@Range(message = "请提交有效的商品类别ID值！") Long categoryId,
                                       @Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【根据商品类别查询商品列表】的请求，父级商品：{}，页码：{}", categoryId, page);
        Integer pageNum = page == null ? 1 : page;
        PageData<GoodsListItemVO> pageData = goodsService.listByCategoryId(categoryId, pageNum);
        return JsonResult.ok(pageData);
    }




}
